import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';

class BomsNumberItemForm extends StatefulWidget {
  final String title;
  final String content;
  final ValueChanged<int> onNumberCallback;
  final int initialNumber;

  const BomsNumberItemForm(
      {Key key,
      this.title,
      this.content,
      this.onNumberCallback,
      this.initialNumber})
      : super(key: key);

  @override
  _BomsNumberItemFormState createState() => _BomsNumberItemFormState();
}

class _BomsNumberItemFormState extends State<BomsNumberItemForm> {
  TextEditingController controller = TextEditingController(text: '1');

  void _onReduce() {
    int integer = int.parse(controller.text, onError: (e) => 0);
    integer--;
    setTextControllerValue(integer);
  }

  void _onAdd() {
    int integer = int.parse(controller.text, onError: (e) => 0);
    integer++;
    setTextControllerValue(integer);
  }

  void setTextControllerValue(int value) {
    String string = value.toString();
    controller.value = TextEditingValue(
      text: string,
      selection: TextSelection.collapsed(offset: string.length),
    );
  }

  @override
  void initState() {
    super.initState();
    if (widget.initialNumber != null) {
      controller = TextEditingController(text: widget.initialNumber.toString());
    }
    controller.addListener(() {
      int integer = int.parse(controller.text, onError: (e) => null);
      if (integer == null || integer < 0) {
        setTextControllerValue(0);
      } else {
        widget.onNumberCallback?.call(integer);
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (widget.title != null)
              Text(
                widget.title,
                style: TextStyle(
                  color: aceDarkGrayColor,
                  fontSize: getSp(16),
                  fontWeight: FontWeight.w400,
                  height: 16 / 14,
                ),
              ),
            if (widget.title != null)
              SizedBox(
                height: getDp(6),
              ),
            Text.rich(TextSpan(children: [
              TextSpan(
                text: widget.content,
                style: TextStyle(
                  color: FormEditStatusInheritedWidget.ofEnable(context)
                      ? aceBlueColor
                      : aceBlackColor,
                  fontSize: getSp(18),
                  fontWeight: FontWeight.w700,
                  height: 1,
                ),
              ),
            ])),
          ],
        ),
        if (FormEditStatusInheritedWidget.ofEnable(context))
          _buildNumberInput()
        else
          Column(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              if (widget.title != null)
                Text(
                  S.current.quantity,
                  style: TextStyle(
                    color: aceDarkGrayColor,
                    fontSize: getSp(16),
                    fontWeight: FontWeight.w400,
                    height: 16 / 14,
                  ),
                ),
              if (widget.title != null)
                SizedBox(
                  height: getDp(6),
                ),
              Text.rich(TextSpan(children: [
                TextSpan(
                  text: widget.initialNumber.toString(),
                  style: TextStyle(
                    color: FormEditStatusInheritedWidget.ofEnable(context)
                        ? aceBlueColor
                        : aceBlackColor,
                    fontSize: getSp(18),
                    fontWeight: FontWeight.w700,
                    height: 1,
                  ),
                ),
              ])),
            ],
          )
      ],
    );
  }

  _buildNumberInput() => Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          PressedOpacityButton(
            onPressed: _onReduce,
            child: SvgPicture.asset(
              A.assetsImagesStyleMinus,
              width: getDp(24),
              height: getDp(24),
            ),
          ),
          SizedBox(
            width: getDp(65),
            child: TextField(
              inputFormatters: [
                FilteringTextInputFormatter.digitsOnly,
              ],
              scrollPadding: EdgeInsets.zero,
              decoration: InputDecoration(
                border: InputBorder.none,
                enabledBorder: InputBorder.none,
              ),
              controller: controller,
              style: TextStyle(
                color: aceBlueColor,
                fontSize: getSp(16),
                fontWeight: FontWeight.w700,
              ),
              textAlign: TextAlign.center,
            ),
          ),
          PressedOpacityButton(
            onPressed: _onAdd,
            child: SvgPicture.asset(
              A.assetsImagesStylePlus,
              width: getDp(24),
              height: getDp(24),
            ),
          ),
        ],
      );

  @override
  void didUpdateWidget(covariant BomsNumberItemForm oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.initialNumber != widget.initialNumber) {
      controller.text = widget.initialNumber.toString();
    }
  }
}
